/* eslint-disable object-curly-newline */
import React, { Component } from 'react';
import { message } from 'antd';
import * as config from '../../utils/config';
import * as utils from '../../utils/utils';
import * as commonServices from '../../services/services';

export default (ChildComponent) => {
  return class Base extends Component {
    constructor(props) {
      super(props);
      this.state = {
        pageLoading: true,
        token: props.app.token,
        sTabId: this.handleConfigValue('key'),
        sModelsId: this.handleConfigValue('formId'), /* 获取配置数据用的modelId */
        charConfigAndData: [], /* 总图表配置及数据 */
        charConfigList: [], /* 配置集 */
        gdsformconst: [], /* 常量 */
        charMenu: [], /* 图表右查询菜单 */
      };
    }

    componentWillMount() {
      /* 获取配置 */
      this.handleCharConfig();
    }

    shouldComponentUpdate(nextProps, nextState) {
      const currentKey = nextProps.app.currentPane.key; /* 当前页签key */
      return currentKey === nextState.sTabId;
    }

    /* 获取配置数据用的sTabId,formId */
    handleConfigValue = (value) => {
      const { secondppopupPane, ppopupPane, currentPane } = this.props.app;
      const data = utils.isNotEmptyObject(secondppopupPane)
        ? secondppopupPane[value]
        : utils.isNotEmptyObject(ppopupPane)
          ? ppopupPane[value] : currentPane[value];
      return data;
    };

    /* 获取图表配置 */
    handleCharConfig = async () => {
      const { token, sModelsId } = this.state;
      const configUrl = `${config.server_host}char/getCharConfig/${sModelsId}?sModelsId=${sModelsId}`;
      const { data } = await commonServices.getService(token, configUrl);
      if (data.code === 1) {
        const [configData] = data.dataset.rows;
        this.handleGetData(configData);
      } else {
        message.error(data.msg);
      }
    };

    /* 获取数据 */
    handleGetData = (configData) => {
      const { charConfigList } = configData; /* 图标配置集 */
      const { charConfigAndData } = this.state;
      const charMenu = [];
      for (const each of charConfigList) {
        const { sId } = each;
        const childConfig = utils.isNotEmptyArr(each.childConfig) ? each.childConfig : [];
        const radioType = utils.isNotEmptyArr(childConfig) ? childConfig[0].sTypeValue : null;
        charMenu.push({ sId, childConfig, radioType });
        charConfigAndData.push({ ...each, child: {} });
        this.handleEachChar(each);
      }
      this.setState({ ...configData, charMenu });
    };

    /* 获取单个图表数据 */
    handleEachChar = async (each) => {
      const { token, sModelsId, charConfigAndData } = this.state;
      const sSqlCondition = utils.isNotEmptyObject(each.sSqlCondition) ? each.sSqlCondition : {};
      const value = { sProcedureNo: each.sProcedureNo, sSqlCondition };
      const url = `${config.server_host}char/getCharData?sModelsId=${sModelsId}`;
      const { data } = await commonServices.postValueService(token, value, url);
      let child = [];
      if (data.code === 1) {
        [child] = data.dataset.rows;
        const index = charConfigAndData.findIndex(item => item.sProcedureNo === each.sProcedureNo);
        if (index > -1) {
          charConfigAndData[index].child = child;
        }
      } else {
        message.error(data.msg);
      }
      this.setState({ charConfigAndData });
    };

    render() {
      return (
        <ChildComponent
          {...this.props}
          {...this.state}
          onEachChar={this.handleEachChar}
        />
      );
    }
  };
};

